package com.zyj.myapplication.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.zyj.myapplication.Activity.ShaitufabuActivity;
import com.zyj.myapplication.Adapter.ShaituListAdapter;
import com.zyj.myapplication.Javabean.Shaitu;
import com.zyj.myapplication.Model.ShaituModel;
import com.zyj.myapplication.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ShaituFragment extends Fragment {
    private RecyclerView recyclerView;
    private ImageView shaitu_fb;
    private Shaitu getAllshaitu;
    private View view = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_shaitu, container, false);
        initViews();//初始化
        initEvents();//初始化事件
        return view;
    }


    private void initViews() {
        //设置布局
        final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        shaitu_fb = (ImageView) view.findViewById(R.id.shaitu_fb);
        recyclerView = (RecyclerView) view.findViewById(R.id.shaitu_list);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }


    private void initEvents() {
        shaitu_fb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ShaitufabuActivity.class);
                startActivityForResult(intent, 1);
                intent.getExtras();
            }
        });

        ShaituModel shaitumodel = new ShaituModel();
        Call call = shaitumodel.getAllshaitu();
        call.enqueue(new Callback<List<Shaitu>>() {
            @Override
            public void onResponse(Call<List<Shaitu>> call, Response<List<Shaitu>> response) {
                List<Shaitu> shaitulist = response.body();
                recyclerView.setAdapter(new ShaituListAdapter(getActivity(), shaitulist));
            }

            @Override
            public void onFailure(Call<List<Shaitu>> call, Throwable t) {

            }
        });

    }
}
